DOCKER_USERNAME="json520"
DOCKER_PASSWORD="jingxiangmima520"
IMAGE_NAME="registry.cn-guangzhou.aliyuncs.com/go-deploy/gf-admin:latest"
CONTAINER_NAME="gf-admin-container"
PORT="10001"  # 设置容器的暴露端口

# 登录到 Docker 仓库
echo "登录到 Docker 仓库..."
docker login --username=${DOCKER_USERNAME} --password=${DOCKER_PASSWORD} registry.cn-guangzhou.aliyuncs.com

# 检查登录是否成功
if [ $? -ne 0 ]; then
	  echo "Docker 登录失败!"
	    exit 1
    fi

    # 拉取镜像
    echo "拉取镜像 ${IMAGE_NAME}..."
    docker pull ${IMAGE_NAME}

    # 检查镜像是否拉取成功
    if [ $? -ne 0 ]; then
	      echo "镜像拉取失败!"
	        exit 1
	fi

	# 检查容器是否已经存在
	if [ "$(docker ps -aq -f name=${CONTAINER_NAME})" ]; then
		  echo "容器 ${CONTAINER_NAME} 已存在，正在停止并删除该容器..."
		    
		    # 停止并删除容器
		      docker stop ${CONTAINER_NAME}
		        docker rm ${CONTAINER_NAME}

			  # 检查删除是否成功
			    if [ $? -ne 0 ]; then
				        echo "容器删除失败!"
					    exit 1
					      fi
				      else
					        echo "容器 ${CONTAINER_NAME} 不存在，跳过删除步骤。"
					fi

					# 运行新的容器
					echo "运行新的容器 ${CONTAINER_NAME} 并将端口 ${PORT} 暴露..."
				docker run -d --network deployment_backend --name ${CONTAINER_NAME} -p ${PORT}:${PORT} ${IMAGE_NAME}

				# 检查容器是否启动成功
				if [ $? -ne 0 ]; then
					  echo "容器启动失败!"
					    exit 1
				    fi

				    echo "容器 ${CONTAINER_NAME} 已成功启动并绑定到端口 ${PORT}."


